home *** CD-ROM | disk | FTP | other *** search
/ X User Tools / X User Tools (O'Reilly and Associates)(1994).ISO / sources / xload / xload.c < prev    next >
C/C++ Source or Header  |  1994-09-27  |  8KB  |  281 lines

  1. /*
  2.  * xload - display system load average in a window
  3.  *
  4.  * Copyright 1989 Massachusetts Institute of Technology
  5.  *
  6.  * $XConsortium: xload.c,v 1.36 91/05/24 16:57:46 converse Exp $
  7.  */
  8.  
  9. #include <stdio.h> 
  10. #include <X11/Intrinsic.h>
  11. #include <X11/Xatom.h>
  12. #include <X11/StringDefs.h>
  13. #include <X11/Shell.h>
  14.  
  15. #include <X11/Xaw/Cardinals.h>
  16. #include <X11/Xaw/Label.h>
  17. #include <X11/Xaw/Paned.h>
  18. #include <X11/Xaw/StripChart.h>
  19. #include <X11/Xmu/SysUtil.h>
  20.  
  21. #include "xload.bit"
  22.  
  23. char *ProgramName;
  24.  
  25. extern void exit(), GetLoadPoint();
  26. static void quit();
  27. static void ClearLights();
  28. static void SetLights();
  29.  
  30. /*
  31.  * Definition of the Application resources structure.
  32.  */
  33.  
  34. typedef struct _XLoadResources {
  35.   Boolean show_label;
  36.   Boolean use_lights;
  37. } XLoadResources;
  38.  
  39. /*
  40.  * Command line options table.  Only resources are entered here...there is a
  41.  * pass over the remaining options after XtParseCommand is let loose. 
  42.  */
  43.  
  44. static XrmOptionDescRec options[] = {
  45.  {"-scale",        "*load.minScale",    XrmoptionSepArg,    NULL},
  46.  {"-update",        "*load.update",        XrmoptionSepArg,    NULL},
  47.  {"-hl",        "*load.highlight",    XrmoptionSepArg,    NULL},
  48.  {"-highlight",        "*load.highlight",    XrmoptionSepArg,    NULL},
  49.  {"-label",        "*label.label",        XrmoptionSepArg,    NULL},
  50.  {"-nolabel",        "*showLabel",            XrmoptionNoArg,       "False"},
  51.  {"-lights",        "*useLights",        XrmoptionNoArg,          "True"},
  52.  {"-jumpscroll",    "*load.jumpScroll",    XrmoptionSepArg,    NULL},
  53. };
  54.  
  55. /*
  56.  * The structure containing the resource information for the
  57.  * Xload application resources.
  58.  */
  59.  
  60. #define Offset(field) (XtOffsetOf(XLoadResources, field))
  61.  
  62. static XtResource my_resources[] = {
  63.   {"showLabel", XtCBoolean, XtRBoolean, sizeof(Boolean),
  64.      Offset(show_label), XtRImmediate, (XtPointer) TRUE},
  65.   {"useLights", XtCBoolean, XtRBoolean, sizeof(Boolean),
  66.     Offset(use_lights), XtRImmediate, (XtPointer) FALSE},
  67. };
  68.  
  69. #undef Offset
  70.  
  71. static XLoadResources resources;
  72.  
  73. static XtActionsRec xload_actions[] = {
  74.     { "quit",    quit },
  75. };
  76. static Atom wm_delete_window;
  77. static int light_update = 10 * 1000;
  78.  
  79. /*
  80.  * Exit with message describing command line format.
  81.  */
  82.  
  83. void usage()
  84. {
  85.     fprintf (stderr, "usage:  %s [-options ...]\n\n", ProgramName);
  86.     fprintf (stderr, "where options include:\n");
  87.     fprintf (stderr,
  88.       "    -display dpy            X server on which to display\n");
  89.     fprintf (stderr,
  90.       "    -geometry geom          size and location of window\n");
  91.     fprintf (stderr, 
  92.       "    -fn font                font to use in label\n");
  93.     fprintf (stderr, 
  94.       "    -scale number           minimum number of scale lines\n");
  95.     fprintf (stderr, 
  96.       "    -update seconds         interval between updates\n");
  97.     fprintf (stderr,
  98.       "    -label string           annotation text\n");
  99.     fprintf (stderr, 
  100.       "    -bg color               background color\n");
  101.     fprintf (stderr, 
  102.       "    -fg color               graph color\n");
  103.     fprintf (stderr, 
  104.       "    -hl color               scale and text color\n");
  105.     fprintf (stderr, 
  106.       "    -nolabel                removes the label from above the chart.\n");
  107.     fprintf (stderr, 
  108.       "    -jumpscroll value       number of pixels to scroll on overflow\n");
  109.     fprintf (stderr, "\n");
  110.     exit(1);
  111. }
  112.  
  113. void main(argc, argv)
  114.     int argc;
  115.     char **argv;
  116. {
  117.     XtAppContext app_con;
  118.     Widget toplevel, load, pane, label_wid, load_parent;
  119.     Arg args[1];
  120.     Pixmap icon_pixmap = None;
  121.     char *label, host[256];
  122.  
  123.     ProgramName = argv[0];
  124.  
  125.     /* For security reasons, we reset our uid/gid after doing the necessary
  126.        system initialization and before calling any X routines. */
  127.     InitLoadPoint();
  128.     setgid(getgid());        /* reset gid first while still (maybe) root */
  129.     setuid(getuid());
  130.  
  131.     toplevel = XtAppInitialize(&app_con, "XLoad", options, XtNumber(options),
  132.                    &argc, argv, NULL, NULL, (Cardinal) 0);
  133.     if (argc != 1) usage();
  134.  
  135.     XtGetApplicationResources( toplevel, (XtPointer) &resources, 
  136.                   my_resources, XtNumber(my_resources),
  137.                   NULL, (Cardinal) 0);
  138.     
  139.     if (resources.use_lights)
  140.     {
  141.     char        name[1024];
  142.     XrmString   type;
  143.     XrmValue    db_value;
  144.     XrmValue    int_value;
  145.     Bool        found = False;
  146.  
  147.     (void) sprintf (name, "%s.paned.load.update", XtName(toplevel));
  148.     found = XrmGetResource (XtScreenDatabase(XtScreen(toplevel)),
  149.                 name, "XLoad.Paned.StripChart.Interval",
  150.                 &type, &db_value);
  151.     if (found) {
  152.         int_value.size = sizeof(int);
  153.         int_value.addr = (XPointer) &light_update;
  154.         found = XtConvertAndStore(toplevel, type, &db_value, XtRInt,
  155.                       &int_value);
  156.         if (found) light_update *= 1000;
  157.     }
  158.     ClearLights (XtDisplay (toplevel));
  159.     SetLights ((XtPointer) toplevel, (XtIntervalId *) 0);
  160.     }
  161.     else
  162.     {
  163.         /*
  164.           * This is a hack so that f.delete will do something useful in this
  165.           * single-window application.
  166.           */
  167.         XtAppAddActions (app_con, xload_actions, XtNumber(xload_actions));
  168.         XtOverrideTranslations(toplevel,
  169.                 XtParseTranslationTable ("<Message>WM_PROTOCOLS: quit()"));
  170.     
  171.         XtSetArg (args[0], XtNiconPixmap, &icon_pixmap);
  172.         XtGetValues(toplevel, args, ONE);
  173.         if (icon_pixmap == None) {
  174.         XtSetArg(args[0], XtNiconPixmap, 
  175.              XCreateBitmapFromData(XtDisplay(toplevel),
  176.                               XtScreen(toplevel)->root,
  177.                               (char *)xload_bits,
  178.                               xload_width, xload_height));
  179.         XtSetValues (toplevel, args, ONE);
  180.         }
  181.     
  182.         if (resources.show_label) {
  183.             pane = XtCreateManagedWidget ("paned", panedWidgetClass,
  184.                         toplevel, NULL, ZERO);
  185.     
  186.             label_wid = XtCreateManagedWidget ("label", labelWidgetClass,
  187.                          pane, NULL, ZERO);
  188.             
  189.             XtSetArg (args[0], XtNlabel, &label);
  190.             XtGetValues(label_wid, args, ONE);
  191.             
  192.             if ( strcmp("label", label) == 0 ) {
  193.         (void) XmuGetHostname (host, 255);
  194.         XtSetArg (args[0], XtNlabel, host);
  195.         XtSetValues (label_wid, args, ONE);
  196.             }
  197.     
  198.             load_parent = pane;
  199.         }
  200.         else
  201.             load_parent = toplevel;
  202.     
  203.         load = XtCreateManagedWidget ("load", stripChartWidgetClass,
  204.                       load_parent, NULL, ZERO);    
  205.     
  206.         XtAddCallback(load, XtNgetValue, GetLoadPoint, NULL);
  207.     
  208.         XtRealizeWidget (toplevel);
  209.         wm_delete_window = XInternAtom (XtDisplay(toplevel), "WM_DELETE_WINDOW",
  210.                         False);
  211.         (void) XSetWMProtocols (XtDisplay(toplevel), XtWindow(toplevel),
  212.                     &wm_delete_window, 1);
  213.     }
  214.     XtAppMainLoop(app_con);
  215. }
  216.  
  217. static unsigned long    current_leds;
  218.  
  219. static void
  220. ClearLights (dpy)
  221.     Display *dpy;
  222. {
  223.     XKeyboardControl    cntrl;
  224.  
  225.     cntrl.led_mode = LedModeOff;
  226.     XChangeKeyboardControl (dpy, KBLedMode, &cntrl);
  227.     current_leds = 0;
  228. }
  229.  
  230. static void
  231. SetLights (data, timer)
  232.     XtPointer        data;
  233.     XtIntervalId    *timer;
  234. {
  235.     Widget        toplevel;
  236.     Display        *dpy;
  237.     double        value;
  238.     unsigned long    new_leds, change, bit;
  239.     int            i;
  240.     XKeyboardControl    cntrl;
  241.  
  242.     toplevel = (Widget) data;
  243.     dpy = XtDisplay (toplevel);
  244.     GetLoadPoint (toplevel, (XtPointer) 0, (XtPointer) &value);
  245.     new_leds = (1 << (int) (value + 0.1)) - 1;
  246.     change = new_leds ^ current_leds;
  247.     i = 1;
  248.     bit = 1;
  249.     while (current_leds != new_leds)
  250.     {
  251.     if (change & bit)
  252.     {
  253.         cntrl.led = i;
  254.         cntrl.led_mode = new_leds & bit ? LedModeOn : LedModeOff;
  255.         XChangeKeyboardControl (dpy, KBLed|KBLedMode, &cntrl);
  256.         current_leds ^= bit;
  257.     }
  258.     i++;
  259.     bit <<= 1;
  260.     }
  261.     XtAppAddTimeOut(XtWidgetToApplicationContext(toplevel), light_update,
  262.             SetLights, data);
  263. }
  264.  
  265. static void quit (w, event, params, num_params)
  266.     Widget w;
  267.     XEvent *event;
  268.     String *params;
  269.     Cardinal *num_params;
  270. {
  271.     if (event->type == ClientMessage &&
  272.         event->xclient.data.l[0] != wm_delete_window) {
  273.         XBell (XtDisplay(w), 0);
  274.         return;
  275.     }
  276.     if (resources.use_lights)
  277.     ClearLights (XtDisplay (w));
  278.     XtDestroyApplicationContext(XtWidgetToApplicationContext(w));
  279.     exit (0);
  280. }
  281.